//
//  RxCellViewModel.swift
//  RxSwiftExample
//
//  Created by LF on 2021/11/28.
//

import UIKit


/// ViewModel 数据绑定
class RxCellViewModel: NSObject {
    
    let name: Driver<String>
    
    let singer: Driver<String>
    
    let model: RxTableModel
    
    lazy var disposeBag = DisposeBag()
    
    init(model: RxTableModel) {
        
        // 创建Driver
        let driver = Driver.of(model)
        
        // driver 赋值给 output属性
        name = driver.map({ $0.name })
        singer = driver.map({ $0.singer })
        self.model = model
    }
    
}

extension RxCellViewModel {
    
    // viewModels
    class func localViewModels() -> Array<RxCellViewModel> {
        
        var viewModels: Array<RxCellViewModel> = []
        
        for i in 0 ..< 10 {
            let model = RxTableModel(name: "歌手\(i)", singer: "歌曲——\(i)")
            let viewModel = RxCellViewModel(model: model)
            viewModels.append(viewModel)
        }
        
        return viewModels
    }
    
}
